<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" :permission="permissionList" :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave" @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">
      <template slot="searchMenu">
        <el-button size="small" type="primary" plain @click="changeNumber(0)">全部使用系统编号</el-button>
        <el-button size="small" type="primary" plain class="ml30" @click="changeNumber(1)">全部使用自定义编号</el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getList, getDetail, add, update, remove, piliangupdate } from '@/api/module/numberset'
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      numberType: '',
      form: {},
      query: {
        ascs: 'sort',
      },
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        labelWidth: 130,
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        searchShow: true,
        searchMenuSpan: 16,
        selection: true,
        dialogClickModal: false,
        column: [
          {
            label: '名称',
            prop: 'name',
            search: true,
            searchSpan:8,
            placeholder: '请输入',
            rules: [
              {
                required: true,
                message: '请输入名称',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '文件编号',
            prop: 'fileNumberType',
            type: 'select',
            display: true,
            placeholder: '请选择',
            dicData: [
              {
                label: '使用系统编号',
                value: 0,
              },
              {
                label: '使用自定义编号',
                value: 1,
              },
            ],
            formatter: (row) => {
              if (row.processType > 5) {
                return '-'
              } else {
                return row.fileNumberType == 0 ? '使用系统编号' : '使用自定义编号'
              }
            },
            rules: [
              {
                required: true,
                message: '请选择',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '文件版本号',
            prop: 'versionNumberType',
            type: 'select',
            display: true,
            placeholder: '请选择',
            dicData: [
              {
                label: '使用系统版本号',
                value: 0,
              },
              {
                label: '使用自定义版本号',
                value: 1,
              },
            ],
            formatter: (row) => {
              if (row.processType > 5) {
                return '-'
              } else {
                return row.versionNumberType == 0 ? '使用系统版本号' : '使用自定义版本号'
              }
            },
            rules: [
              {
                required: true,
                message: '请选择',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '管理编号',
            prop: 'numberType',
            type: 'select',
            display: true,
            placeholder: '请选择',
            dicData: [
              {
                label: '使用系统编号',
                value: 0,
              },
              {
                label: '使用自定义编号',
                value: 1,
              },
            ],
            formatter: (row) => {
              if (row.processType > 5) {
                return row.numberType == 0 ? '使用系统编号' : '使用自定义编号'
              } else {
                return '-'
              }
            },
            rules: [
              {
                required: true,
                message: '请选择',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '生产批号',
            prop: 'batchNumberType',
            type: 'select',
            display: true,
            placeholder: '请选择',
            dicData: [
              {
                label: '使用系统编号',
                value: 0,
              },
              {
                label: '使用自定义编号',
                value: 1,
              },
            ],
            formatter: (row) => {
              if (row.processType == 26) {
                return row.batchNumberType == 0 ? '使用系统编号' : '使用自定义编号'
              } else {
                return '-'
              }
            },
            rules: [
              {
                required: true,
                message: '请选择',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '原材料批号',
            prop: 'materialNumberType',
            type: 'select',
            display: true,
            placeholder: '请选择',
            dicData: [
              {
                label: '使用系统编号',
                value: 0,
              },
              {
                label: '使用自定义编号',
                value: 1,
              },
            ],
            formatter: (row) => {
              if (row.processType == 24) {
                return row.materialNumberType == 0 ? '使用系统编号' : '使用自定义编号'
              } else {
                return '-'
              }
            },
            rules: [
              {
                required: true,
                message: '请选择',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '灭菌批号',
            prop: 'sterilizeNumberType',
            type: 'select',
            display: true,
            placeholder: '请选择',
            dicData: [
              {
                label: '使用系统编号',
                value: 0,
              },
              {
                label: '使用自定义编号',
                value: 1,
              },
            ],
            formatter: (row) => {
              if (row.processType == 29) {
                return row.sterilizeNumberType == 0 ? '使用系统编号' : '使用自定义编号'
              } else {
                return '-'
              }
            },
            rules: [
              {
                required: true,
                message: '请选择',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '记录流水号',
            prop: 'serialNumberType',
            type: 'select',
            display: true,
            placeholder: '请选择',
            dicData: [
              {
                label: '使用系统流水号',
                value: 0,
              },
              {
                label: '使用自定义流水号',
                value: 1,
              },
            ],
            formatter: (row) => {
              if (row.processType > 5) {
                return row.serialNumberType == 0 ? '使用系统流水号' : '使用自定义流水号'
              } else {
                return '-'
              }
            },
            rules: [
              {
                required: true,
                message: '请选择',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '排序',
            prop: 'sort',
            hide: true,
            display: false,
            placeholder: '请输入',
            rules: [
              {
                required: true,
                message: '请输入排序',
                trigger: 'blur',
              },
            ],
          },
        ],
      },
      data: [],
    }
  },
  watch: {
    form(newval) {
      const fileNumberType = this.findObject(this.option.column, 'fileNumberType')
      const numberType = this.findObject(this.option.column, 'numberType')
      const serialNumberType = this.findObject(this.option.column, 'serialNumberType')
      const versionNumberType = this.findObject(this.option.column, 'versionNumberType')
      const materialNumberType = this.findObject(this.option.column, 'materialNumberType')
      const sterilizeNumberType = this.findObject(this.option.column, 'sterilizeNumberType')
      const batchNumberType = this.findObject(this.option.column, 'batchNumberType')
      if (newval.processType) {
        if (Number(newval.processType) <= 5) {
          fileNumberType.display = true
          versionNumberType.display = true
          serialNumberType.display = false
          numberType.display = false
          sterilizeNumberType.display = false
          materialNumberType.display = false
          batchNumberType.display = false
        } else {
          numberType.display = true
          serialNumberType.display = true
          versionNumberType.display = false
          fileNumberType.display = false
          if (Number(newval.processType) == 24) {
            materialNumberType.display = true
          } else {
            materialNumberType.display = false
          }
          if (Number(newval.processType) == 26) {
            batchNumberType.display = true
          } else {
            batchNumberType.display = false
          }
          if (Number(newval.processType) == 29) {
            sterilizeNumberType.display = true
          } else {
            sterilizeNumberType.display = false
          }
        }
      }
    },
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.numberset_add, false),
        viewBtn: this.vaildData(this.permission.numberset_view, false),
        delBtn: this.vaildData(this.permission.numberset_delete, false),
        editBtn: this.vaildData(this.permission.numberset_edit, false),
      }
    },
    ids() {
      let ids = []
      this.selectionList.forEach((ele) => {
        ids.push(ele.id)
      })
      return ids.join(',')
    },
  },
  methods: {
    changeNumber(type, loading) {
      piliangupdate({ fileNumberType: type }).then(
        () => {
          this.onLoad(this.page)
        },
        (error) => {
          loading()
          window.console.log(error)
        }
      )
    },
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!',
          })
          done()
        },
        (error) => {
          loading()
          window.console.log(error)
        }
      )
    },
    rowUpdate(row, index, done, loading) {
      console.log(row)
      if (row.processType > 5) {
        row.fileNumberType = row.numberType
      }
      update(row).then(
        () => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!',
          })
          done()
        },
        (error) => {
          loading()
          console.log(error)
        }
      )
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return remove(row.id)
        })
        .then(() => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!',
          })
        })
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据')
        return
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return remove(this.ids)
        })
        .then(() => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!',
          })
          this.$refs.crud.toggleSelection()
        })
    },
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        getDetail(this.form.id).then((res) => {
          res.data.data.numberType = res.data.data.fileNumberType
          this.form = res.data.data
        })
      }
      done()
    },
    searchReset() {
      this.query = {}
      this.onLoad(this.page)
    },
    searchChange(params, done) {
      this.query = params
      this.page.currentPage = 1
      this.onLoad(this.page, params)
      done()
    },
    selectionChange(list) {
      this.selectionList = list
    },
    selectionClear() {
      this.selectionList = []
      this.$refs.crud.toggleSelection()
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    refreshChange() {
      this.onLoad(this.page, this.query)
    },
    onLoad(page, params = {}) {
      this.loading = true
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data
        data.records.forEach((item) => (item.numberType = item.fileNumberType))
        this.page.total = data.total
        this.data = data.records
        this.loading = false
        this.selectionClear()
      })
    },
  },
}
</script>

<style scoped>
::v-deep .avue-group{
    margin: 0 auto;
  }
</style>
